import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import AddEditModal from '#cpt/temp/AddEditModal'
import Enable from '#cpt/temp/Enable'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== $Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== data
const typeStatus = [{ name:'普通', value:'1' },{ name:'高级', value:'2' }]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '状态',	name: 'gp_status',	type:'select',		loading:true },
			{ label: '名称',	name: 'name'},
			{ label: '科室',	name: 'job_id',		type:'select',		loading:true, 	idStr:'uuid'},
		],
		// 提交表单列表
		formData : [
			{ label: '分拣组名称',	name: 'gp_name', 		half:true, 	required:true},
			{ label: '拼音简称',		name: 'pinyin_short', 	half:true },
			{ label: '类型',		name: 'gp_type', 		half:true,  type: 'select',	required:true, data:typeStatus },
			{ label: '科室',		name: 'job_id', 		half:true,  type: 'select', required:true, loading:true, idStr:'uuid' },
			{ label: '备注',		name: 'remark', 		full:true, 	type:'textarea' },
		]
	}
	submit = {}
	
	componentDidMount(){
		this.getSelect()
		if(window.isInit){
			this.fetch()
		}
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { forms, formData } = this.state
	
		$cache.then( async f => {
			// 科室
			$fn.setRows([forms, formData], 'job_id', await f.getCache({ api:'ke_shi' }))
			// 状态
			const gpStatus =  $fn.setRow(forms, 'gp_status', await f.company_state )
			//
			this.setState({ forms, formData, gpStatus })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'pick-group/index')
	
	// 表单
	cols = [
		{ title: '分拣组代码', 	dataIndex: 'gp_code', 		...$fn.table.spec_code },
		{ title: '分拣组名称', 	dataIndex: 'gp_name', 		width:150 },
		{ title: '科室', 		dataIndex: 'job_name', 		width:100 },
		{ title: '备注', 		dataIndex: 'remark',  		width:100},
		{ title: '状态', 		dataIndex: 'gp_status',  	...$fn.table.status,	render: text => $fn.getState(this.state.gpStatus,text)},
		TableControls.controls.call(this,{ width: 100 })
	]
	
	// 表格操作按钮
	table_controls = (text,{uuid,gp_status}) => {
		return [
			TableControls.edit.call(this, 'pick-group/info', { uuid },{ 		// 编辑
				load: ({ id, uuid, gp_number, gp_code, gp_status, sort, created_at, comp_id })=> {
					// this.submit = {id, uuid, gp_number, gp_code, gp_status, sort, created_at, comp_id}
				}
			}),	
			this.onDel([ uuid ]),														// 删除
			+gp_status === 0 && this.onEnabled(1, [ uuid ]),							// 启用
			+gp_status === 1 && this.onEnabled(0, [ uuid ]),							// 禁用
		]
	}
	
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this),   			// 新增
			this.onEnabled(1),	// 启用
			this.onEnabled(0),	// 禁用
			this.onDel(),		// 删除
		]
	}
	
	// 删除
	onDel = uuid => Delete.call(this, 'pick-group/del', { uuid, isKeys: !uuid })
	
	// 禁用/启用
	onEnabled = (enabled, uuid) => Enable.call(this, enabled,  { uuid, on:'pick-group/on', off:'pick-group/off', isKeys: !uuid })
		
	render(){
		return (
			<>
				<Page title='分拣管理' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, {isInit: window.isInit} ) }
				</Page>
				{/* 添加 编辑 */}
				{AddEditModal.call(this,{title:'分拣管理', labelWidth:'s6', width:800, api:'pick-group/add', editApi:'pick-group/edit' })}
			</>
		)
	}
}

export default Index